#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "getputch.h"

#define MAX_STAGE 20
#define swap(type, x, y) do {type z=x; x=y; y=z;} while(0)

int main(void)
{

    srand(time(NULL));
    init_getputch();

    char *qstr[] = {"0123456789", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"};

    int chmax[] = {10, 26, 26};

    puts("𑀩 请输入连续的3个数字或英文字母(区分大小写)中被隐去的字符.");
    puts("𑀩 例如:\n  A?C:C;\n  45?:6");
    printf("𑀩 按下空格銉开始.");
    fflush(stdout);

    while (getch() != ' ');

    time_t s, e;

    time(&s);

    putchar('\n');

    int stage =0;
    for (;stage < MAX_STAGE; ++stage) {
        int ch_t = rand() % 3;

        int ch_s = rand() % (chmax[ch_t] - 2);

        int ch_q = rand() % 3;

        int i=0;

        for (; i<3; ++i) {
            putchar(i == ch_q ? '?' : qstr[ch_t][ch_s + i]);
        }

        putch(':');

        char goal = qstr[ch_t][ch_s + ch_q];
        char in;
        do {
            in = getch();
            putch(in);
            if (in != goal) {
                printf("\a\b");
                fflush(stdout);
            }
        } while (in != goal);
        putch('\r');
        putch('\n');
    }

    time(&e);

    time_t dur = e - s;

    printf("总共用了%lu\n", dur);

    if (dur > 50) {
        puts("反应太慢了");

    } else if (dur > 40) {
        puts("反应有点慢");

    } else if (dur > 34) {
        puts("反应还行");

    } else {
        puts("反应真快");
    }

    fflush(stdout);

    getch();

    term_getputch();

    return 0;
}
